package com.niodata.dp.task.service.impl;

import com.niodata.dp.core.OwnerType;
import com.niodata.dp.core.resource.AbstractResourceDetailResolver;
import com.niodata.dp.core.resource.ResourceDetail;
import com.niodata.dp.core.resource.ResourceType;
import com.niodata.dp.core.usermgr.model.UserGroup;
import com.niodata.dp.core.usermgr.op.GroupInfoService;
import com.niodata.dp.task.dao.TaskConfMapper;
import com.niodata.dp.task.entity.TaskConf;
import com.niodata.dp.task.service.TaskConfManager;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by ndp on 17-8-17.
 */
@Service("taskConfManagerImpl")
@Transactional
public class TaskConfManagerImpl extends AbstractResourceDetailResolver implements TaskConfManager {

  @Autowired
  TaskConfMapper taskConfMapper;

  @Autowired
  @Qualifier("DpUserMgrGroup")
  private GroupInfoService groupOperation;

  @Override
  public void createTaskConf(TaskConf taskConf) {
    taskConfMapper.saveTaskConf(taskConf);
  }

  @Override
  public void deleteTaskConfById(String id) {
    taskConfMapper.deleteTaskConf(id);
  }

  @Override
  public void updateTaskConf(TaskConf taskConf) {
    taskConfMapper.updateTaskConf(taskConf);
  }

  @Override
  public TaskConf getTaskConfById(String id) {
    return taskConfMapper.getTaskConfById(id);
  }

  @Override
  public TaskConf getTaskConfByName(String name) {
    return taskConfMapper.getTaskConfByName(name);
  }

  @Override
  public void deleteTaskConfByName(String name) {
    taskConfMapper.deleteTaskConfByName(name);
  }

  @Override
  public List<TaskConf> getUserTaskConf(long userId, String keyWord,
      int offset, int count) {
    return taskConfMapper.findUserTaskConf(userId, keyWord, offset, count);
  }

  @Override
  public List<TaskConf> getGroupTaskConf(long userId, long groupId, String keyWord,
      int offset, int count) {
    return taskConfMapper.findUserAuthorizedGroupTaskConf(userId, groupId, keyWord, offset, count);
  }

  @Override
  public List<TaskConf> getUserAuthorizedTaskConf(long userId, String keyWord,
      int offset, int count) {
    List<UserGroup> userGroups = this.groupOperation.getUserGroups(userId);
    List<Long> ids = new ArrayList<>();
    userGroups.forEach(userGroup -> {
      ids.add(userGroup.getGroupId());
    });
    return taskConfMapper.findUserAuthorizedTaskConf(userId, ids, keyWord, offset, count);
  }

  @Override
  public ResourceDetail doResolve(ResourceType resourceType, String path) {
    TaskConf taskConf = this.taskConfMapper.getTaskConfById(path);
    ResourceDetail detail = new ResourceDetail();
    detail.setPath(path);
    detail.setOwnerId(taskConf.getOwnerId());
    detail.setOwnerType(OwnerType.valueOf(taskConf.getOwnerType()));
    detail.setResourceType(ResourceType.TaskConf);
    return detail;
  }

  @Override
  public boolean supportResourceType(Class<? extends ResourceType> clazz) {
    return ResourceType.TaskConfType.class.equals(clazz);
  }
}
